package com.javabasic2.Shootclient;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;

import javax.swing.JPanel;

public class BulletImage {
	public enum Type {
		ALIVE , BOOM
	};
	private Type type = Type.ALIVE;
	private Image img;
	private int sizeX, sizeY;
	private int x, y;
	private int velocity = 0;
	private int lifePoint = 3;
	private int bulletDamage = 5;
	public int getBulletDamage() {
		return bulletDamage;
	}
	public void setBulletDamage(int bulletDamage) {
		this.bulletDamage = bulletDamage;
	}

	private BoomImage boom;
	public int getLifePoint() {
		return lifePoint;
	}
	public void setLifePoint(int lifePoint) {
		this.lifePoint = lifePoint;
	}

	private JPanel panel;
	private MainFrame frame;
	private boolean enermy;
	private boolean life = true;
	private boolean crash = false;

	public BulletImage(Image img, MainFrame frame, boolean enermy) {
		init();
		this.frame = frame;
		this.panel = frame.getPanel();
		this.enermy = enermy;
		this.img = img;
		boom = new BoomImage(frame.getBoomImage(),frame);
	}

	public int getSizeX() {
		return sizeX;
	}

	public int getSizeY() {
		return sizeY;
	}

	public void setSize(int sizeX, int sizeY) {
		this.sizeX = sizeX;
		this.sizeY = sizeY;
	}

	public int getX() {
		return sizeX;
	}

	public int getY() {
		return sizeY;
	}

	public void setPos(int x, int y) {
		this.x = x;
		this.y = y;
	}
	public Point getCenter() {
		return new Point(x,y);
	}

	public void init() {
	}

	public void draw(Graphics g) {
		switch(type) {
		case ALIVE:
			g.drawImage(img, x - sizeX/2, y - sizeY/2, sizeX, sizeY,
			Color.WHITE, panel);
			y += velocity;
			break;
			
		case BOOM:
			if(boom.isLife())
				boom.draw(g);
			else
				life = false;
			break;
		}
	}

	public void setVelocity(int velocity) {
		this.velocity = velocity;
	}

	public boolean isLife() {
		if(lifePoint <= 0) {
			setVelocity(0);
			boom.setPos(x, y);
			boom.setSize(50, 50);
			type = Type.BOOM;
		}
		if(life) {
			if (x < -100 || x > frame.getWidth() + 100 || y < -100
					|| y > frame.getHeight() + 100)
				life = false;
		}
		return life;
	}
}
